package edu.ua.bmr.android;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.provider.BaseColumns;
import android.util.Log;

public class BMR {

	public static final String DB_NAME = "BMR";
	public static final String DB_TABLE_USERS = "Users";
	public static final int DB_VERSION = 1;
	public static final String TEXT_FORMAT = "UTF-8";
	
	/**
	 * The following constants are some keywords we are expecting 
	 * when the server responds to us. When referencing a table field,
	 * we name its constant TABLE_[Tablename]_[Fieldname].
	 * 
	 * KEYWORD prefixes don't correspond to table fields, but are
	 * "miscellaneous other info" the server will provide to us.
	 */
	public static final String KEYWORD_REGISTERED = "registered";
	public static final String KEYWORD_CONFIRMED = "confirmed";
	public static final String KEYWORD_RESULT = "result";
	public static final String KEYWORD_MSG = "msg";
	public static final String TABLE_CLASSES_CRN = "crn";
	public static final String TABLE_CLASSES_CWID = "cwid";
	public static final String TABLE_SEMESTER_ADDABLE = "add";
	public static final String TABLE_SEMESTER_DROPPABLE = "drop";
	public static final String TABLE_SEMESTER_ID = "id";
	public static final String TABLE_SEMESTER_NAME = "name";
	
	public static final String CLASSLIST_FIELDS[] = {"title", "dept", "code", "section", "instructor", "times"};
	public static final int CLASSLIST_VIEWS[] = {R.id.classitem_title, R.id.classitem_department, R.id.classitem_number, R.id.classitem_section, R.id.classitem_instructor, R.id.classitem_time};
	
	/* Establish a list of constants we can use for the threads to pass to handlers */
	
	/** Passed when the request to the Session object ended with an exception */
	public static final int MESSAGE_EXCEPTION = 0;
	
	/** The action ended with a success. */
	public static final int MESSAGE_SUCCESS = 1;
	
	/** The action ended with a failure */
	public static final int MESSAGE_FAILURE = 2;
	
	/** No problems with the request, but more action is required to find out the result of the request */
	public static final int MESSAGE_LEGAL = 3;
	
	/** Preferences filenames to keep track of which semesters the user knows about */
	public static final String PREFS_SEMESTERS_CHECKED = "CheckedSemesters";
	
	public static final class Users implements BaseColumns {
		public static final String USERNAME = "Username";
		public static final String PASSWORD = "Password";
	}

	private static final String TAG = "BMR";
	
	public static Dialog showProgressDialog(Context context, int resTitle, int resMsg) {
		Resources r = context.getResources();
		Dialog d = ProgressDialog.show(context, r.getString(resTitle), r.getString(resMsg));
		d.setCancelable(true);
		return d;
	}
	
	public static Intent getClassViewIntent(Context context, HashMap<String, String> values) {
		// We're sending the user off to ClassActivity land...
		Intent intent = new Intent(context, ClassActivity.class);
		
		// Populate the intent with the key/value set of the HashMap selected.
		Set<String> keySet = values.keySet();
		Iterator<String> iter = keySet.iterator();
		while(iter.hasNext()) {
			String key = iter.next();
			String value = (String) values.get(key);
			intent.putExtra(key, value);
		}
		
		return intent;
	}
	
	public static void setPreference(Context ctx, String name, String user, String var, boolean flag) {
	      // Load the settings and get an editor.
	      SharedPreferences settings = ctx.getSharedPreferences(name, 0);
	      SharedPreferences.Editor editor = settings.edit();
	      
	      // Amend the var name so that it's unique to each user.
	      editor.putBoolean(user + "-" + var, flag);
	      editor.commit();
	}
	
	/**
	 * Alias to retrieve per-user boolean preference value.
	 * @param ctx context for the preferences
	 * @param name name of preferences file
	 * @param user currently logged in user
	 * @param var variable to set
	 * @return the boolean value requested or false if it doesn't exist.
	 */
	public static boolean getBooleanPreference(Context ctx, String name, String user, String var) {
	      // Load the settings and get an editor.
	      SharedPreferences settings = ctx.getSharedPreferences(name, 0);
	      
	      // Return false if pref doesn't exist or return the value itself.
	      return settings.getBoolean(user + "-" + var, false);
	}
	
	/** DELETE IT ALL */
	public static void debugDeletePrefs(Context ctx) {
		Log.i(TAG, "Deleting all preferences");
		SharedPreferences settings = ctx.getSharedPreferences(PREFS_SEMESTERS_CHECKED, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.clear();
		editor.commit();
	}
}
